﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DA;

namespace BL
{
    public class SlicesRepository
    {
        public IEnumerable<Slice> GetList()
        {
            using (OrdersDeskEntities db = new OrdersDeskEntities())
            {
                try
                {
                    return db.Slices.ToList();
                }
                catch
                {
                    return null;
                }
            }
        }

        public Slice GetSlice(int sliceId)
        {
            using (OrdersDeskEntities db = new OrdersDeskEntities())
            {
                return db.Slices.SingleOrDefault(x => x.SliceId == sliceId);
            }
        }

        public bool AddSlice(Slice newSlice)
        {
            using (OrdersDeskEntities db = new OrdersDeskEntities())
            {
                try
                {
                    db.Slices.AddObject(newSlice);

                    db.SaveChanges();
                }
                catch
                {
                    return false;
                }

                return true;
            }
        }

        public bool UpdateSlice(int sliceId, Slice newSlice)
        {
            using (OrdersDeskEntities db = new OrdersDeskEntities())
            {
                try
                {
                    Slice slice = db.Slices.SingleOrDefault(x => x.SliceId == sliceId);

                    slice.Name = newSlice.Name;
                    slice.Description = newSlice.Description;

                    db.SaveChanges();
                }
                catch
                {
                    return false;
                }

                return true;
            }
        }

        public bool DeleteSlice(int sliceId)
        {
            using (OrdersDeskEntities db = new OrdersDeskEntities())
            {
                try
                {
                    db.Slices.DeleteObject(db.Slices.SingleOrDefault(x => x.SliceId == sliceId));

                    db.SaveChanges();
                }
                catch
                {
                    return false;
                }

                return true;
            }
        }
    }
}
